package com.wpf.system.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wpf.system.entity.*;
import com.wpf.system.entity.vo.BalanceJournalVO;
import com.wpf.system.service.*;
import com.wpf.system.mapper.BalanceJournalMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
* @author ferna
* @description 针对表【balance_journal(金额明细表)】的数据库操作Service实现
* @createDate 2023-02-06 10:43:44
*/
@Service
public class BalanceJournalServiceImpl extends ServiceImpl<BalanceJournalMapper, BalanceJournal> implements BalanceJournalService{
    @Resource
    private AssetService assetService;

    @Resource
    private FieldBookService fieldBookService;

    @Resource
    private EquipmentRentService equipmentRentService;

    @Resource
    private EquipmentBuyService equipmentBuyService;

    @Override
    public void balanceOut(Orders orders) {
        Asset asset = assetService.getAssetByUid(orders.getUid());
        BalanceJournal balanceJournal = new BalanceJournal();
        balanceJournal.setAid(asset.getId());
        balanceJournal.setOid(orders.getId());
        balanceJournal.setType(1);
        balanceJournal.setRemark(orders.getType());
        balanceJournal.setBalance(asset.getBalance().subtract(orders.getCost()));
        balanceJournal.setBalanceChange(orders.getCost());
        this.baseMapper.insert(balanceJournal);
        asset.setBalance(asset.getBalance().subtract(orders.getCost()));
        assetService.updateById(asset);
    }

    @Override
    public void balanceInto(Orders orders, int remark, Long id) {
        Asset asset = assetService.getAssetByUid(orders.getUid());
        BalanceJournal balanceJournal = new BalanceJournal();
        balanceJournal.setAid(asset.getId());
        balanceJournal.setOid(orders.getId());
        balanceJournal.setType(0);
        balanceJournal.setRemark(remark);
        BigDecimal backBalance = null;
        switch (remark){
            case 0:
                //预约金额退还
                FieldBook fieldBook = fieldBookService.getById(id);
                if(orders.getPoint()==0){
                    backBalance = fieldBook.getBookCost();
                }else{
                    //返回该项在订单金额中的占比数
                    backBalance = orders.getCost().multiply(computedBackBalance(orders.getCost(),orders.getPoint(),fieldBook.getBookCost()));
                }
                break;
            case 1:
                //器材租赁金额退还
                EquipmentRent equipmentRent = equipmentRentService.getById(id);
                if(orders.getPoint()==0) {
                    backBalance = equipmentRent.getCost();
                }else{
                    //返回该项在订单金额中的占比数
                    backBalance = orders.getCost().multiply(computedBackBalance(orders.getCost(),orders.getPoint(),equipmentRent.getCost()));
                }
                break;
            case 2:
                //器材购买金额退还
                EquipmentBuy equipmentBuy = equipmentBuyService.getById(id);
                if(orders.getPoint()==0) {
                    backBalance = equipmentBuy.getCost();
                }else{
                    //返回该项在订单金额中的占比数
                    backBalance = orders.getCost().multiply(computedBackBalance(orders.getCost(),orders.getPoint(),equipmentBuy.getCost()));
                }
                break;
            case 3:
                //金额充值
                backBalance = orders.getCost();
                break;
        }
        balanceJournal.setBalance(asset.getBalance().add(backBalance));
        balanceJournal.setBalanceChange(backBalance);
        asset.setBalance(asset.getBalance().add(backBalance));
        this.baseMapper.insert(balanceJournal);
        assetService.updateById(asset);
    }

    @Override
    public List<BalanceJournalVO> getBalanceJournal(Long aid, String type, String startTime, String endTime, Integer page, Integer pageSize) {
        return this.baseMapper.getBalanceJournal(aid,type,startTime,endTime,page,pageSize);
    }

    public BigDecimal computedBackBalance(BigDecimal orderCost,Long orderPoint,BigDecimal itemCost){
        //因积分扣除的金额 Long->double->str->big
        BigDecimal pointCost = new BigDecimal(Convert.toStr(Convert.toDouble(orderPoint)/1000));
        //未使用积分前的金额
        BigDecimal originalCost = orderCost.add(pointCost);
        //单项花费占总花费多少（只取前两位，后面小数忽略）
        return itemCost.divide(originalCost, 2, RoundingMode.DOWN);
    }
}




